package ai;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NeuralNet {
/*
    private int inputSize;
    private boolean[] inputs;
    private List<NeuralNetLayer> layers;

    public NeuralNet(int nrlayers, int[] nrNodes, int inputSize) {
        layers = new LinkedList<NeuralNetLayer>();
        int inSize = inputSize;

        for (int i = 0; i < nrlayers; i++) {
            layers.add(new NeuralNetLayer(nrNodes[i], inSize));
            inSize = nrNodes[i];
        }

    }

    private NeuralNet(List<NeuralNetLayer> layers, int inputSize) {
        this.layers = layers;
        this.inputSize = inputSize;
    }

    /**
     * This method takes the boolean input from the game position and evaluates it to a score
     * @param input
     * @return score
     *
    public float evaluate(boolean[] inputs) {
        boolean[] input, output = null;
        input = inputs;
        ListIterator<NeuralNetLayer> it = layers.listIterator();

        while (it.hasNext()) {
            output = it.next().outputs(input);
            input = output;
        }

        return score(output);
    }

    /**
     *
     * This method converts the boolean output of the NN to a "score" evaluating the given position
     * @param input
     * @return score
     *
    private float score(boolean[] input) {
        return 0;
    }

    //TODO: TEST!!
    public void saveNNState(Long trainingRoundNr) {
        String newline = System.getProperty("line.separator");
        String state = new String();
        state += trainingRoundNr + newline;
        state += layers.size() + newline;

        for (NeuralNetLayer l : layers) {
            state += l.nrOfNodes() + " ";
        }

        state += newline;

        for (NeuralNetLayer l : layers) {
            float[][] allWeights = l.getWeights();
            for (float[] weights : allWeights) {
                state += weights.length;
                for (float f : weights) {
                    state += f + " ";
                }
                state += newline;
            }
            state += newline;
        }

        try {
            FileWriter fw = new FileWriter("net_" + trainingRoundNr + ".nn");
            BufferedWriter out = new BufferedWriter(fw);
            out.write(state);
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getLocalizedMessage());
        }
    }

    // TODO: Hmmm... This is quite possibly proper fucked. TEST!!
    public static NeuralNet restoreNNState(File file) {

        NeuralNet nn = null;

        int nrlayers;
        int nrNodes[];
        int inputSize = 0;

        String newline = System.getProperty("line.separator");
        Scanner fileScanner;

        try {
            fileScanner = new Scanner(file);

            fileScanner.nextLine();
            //TODO: Should get trainingRoundNr from first line?
            nrlayers = fileScanner.nextInt();

            nrNodes = new int[nrlayers];

            List<NeuralNetLayer> layers = new LinkedList<NeuralNetLayer>();

            fileScanner.nextLine();

            for (int i = 0; i < nrlayers; i++) {
                nrNodes[i] = fileScanner.nextInt();
            }

            fileScanner.nextLine();

            boolean firstTime = true;

            for (int i = 0; i < nrlayers; i++) {
                int nrOfWeights = fileScanner.nextInt();
                if (firstTime) {
                    inputSize = nrOfWeights;
                    firstTime = false;
                }

                float[][] layer = new float[nrNodes[i]][nrOfWeights];

                for (int j = 0; j < nrNodes[i]; j++) {
                    for (int k = 0; k < nrOfWeights; k++) {
                        layer[j][k] = fileScanner.nextFloat();
                    }
                    if (fileScanner.nextLine().equals(newline)) {
                        nrOfWeights = fileScanner.nextInt();
                    }
                }

                layers.add(new NeuralNetLayer(layer));

                fileScanner.nextLine();
            }

            nn = new NeuralNet(layers, inputSize);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(NeuralNet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return nn;
    }*/
}
